﻿using System;
using System.Collections.ObjectModel;
using AutoKoC.Models;
using AutoKoC.ViewModels.ItemClasses;
using CSharp.Libs.WPF;



namespace AutoKoC.ViewModels
{
    class AutoTransporterViewModel : ViewModelBase
    {
        #region declarations
        private AppBase _AB = null;

        private ObservableCollection<AutoTransportCityItem> _ocAutoTransportCities = new ObservableCollection<AutoTransportCityItem>();
        #endregion



        #region constructor / terminator
        public AutoTransporterViewModel(
            AppBase AB)
        {
            _AB = AB;

            _AB.TM.KocServerChanged += new TrafficMonitor.KocServerChangedEventHandler(TM_KocServerChanged);
        }

        ~AutoTransporterViewModel()
        {
            if(_AB != null)
            {
                if(_AB.TM != null)
                {
                    _AB.TM.KocServerChanged -= TM_KocServerChanged;
                }
             
                _AB = null;
            }
        }
        #endregion



        #region event handling
        void TM_KocServerChanged(object sender)
        {
            try
            {
                _ocAutoTransportCities.Clear();

                foreach(SeedCityItem SCI in _AB.BSVM.Cities)
                {
                    AutoTransportCityItem ATCI = new AutoTransportCityItem();
                    ATCI.CityID = SCI.CityID;
                    ATCI.CityName = SCI.CityName;
                    ATCI.xCoord = SCI.xCoord;
                    ATCI.yCoord = SCI.yCoord;
                    ATCI.ProvinceID = SCI.ProvinceID;
                    ATCI.MapTileID = SCI.MapTileID;

                    _ocAutoTransportCities.Add(ATCI);
                }

                _AB.DBOps.AutoTransportGetItems(Int32.Parse(_AB.TM.CurrentServerId), ref _ocAutoTransportCities);
            }
            catch(Exception ex)
            {
                _AB.EC.AddException(ex, this.ToString(), _AB.EC.Tools.GetCurrentMethodName());
            }
        }
        #endregion
        


        #region private functions
        #endregion



        #region interface
        #region properties
        public ObservableCollection<AutoTransportCityItem> AutoTransportCities
        {
            get
            {
                return _ocAutoTransportCities;
            }
        }
        #endregion

        #region methods
        #endregion
        #endregion
    }
}
